package com.eastone.user.serverapi.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eastone.user.serverapi.dao.InfoUserRoleDao;
import com.eastone.user.serverapi.model.InfoUserRole;
import com.eastone.user.serverapi.service.InfoUserRoleService;
import com.yjx.framework.cache.ServiceResultCodeCache;
import com.yjx.framework.model.ServiceResult;

@Service("infoUserRoleService")
public class InfoUserRoleServiceImpl implements InfoUserRoleService {
	@Autowired
	private InfoUserRoleDao infoUserRoleDao;
	private static ServiceResultCodeCache resultCode = ServiceResultCodeCache.getInstance();

	@Override
	public InfoUserRole get(int id) {
		return infoUserRoleDao.get(id);
	}

	@Override
	public ServiceResult<String> add(InfoUserRole infoUserRole) {
		ServiceResult<String> serviceResult = new ServiceResult<String>();
		int result = infoUserRoleDao.add(infoUserRole);
		if (result == 1) {
			serviceResult.setCode(resultCode.get("common.sucess"));
		} else {
			serviceResult.setCode(resultCode.get("common.fail"));
		}
		return serviceResult;
	}

	@Override
	public ServiceResult<String> addSelective(InfoUserRole infoUserRole) {
		ServiceResult<String> serviceResult = new ServiceResult<String>();
		int result = infoUserRoleDao.addSelective(infoUserRole);
		if (result == 1) {
			serviceResult.setCode(resultCode.get("common.sucess"));
		} else {
			serviceResult.setCode(resultCode.get("common.fail"));
		}
		return serviceResult;
	}

	@Override
	public ServiceResult<String> update(InfoUserRole infoUserRole) {
		ServiceResult<String> serviceResult = new ServiceResult<String>();
		int result = infoUserRoleDao.update(infoUserRole);
		if (result == 1) {
			serviceResult.setCode(resultCode.get("common.sucess"));
		} else {
			serviceResult.setCode(resultCode.get("common.fail"));
		}
		return serviceResult;
	}

	@Override
	public ServiceResult<String> updateSelective(InfoUserRole infoUserRole) {
		ServiceResult<String> serviceResult = new ServiceResult<String>();
		int result = infoUserRoleDao.updateSelective(infoUserRole);
		if (result == 1) {
			serviceResult.setCode(resultCode.get("common.sucess"));
		} else {
			serviceResult.setCode(resultCode.get("common.fail"));
		}
		return serviceResult;
	}

	@Override
	public ServiceResult<String> delete(InfoUserRole infoUserRole) {
		ServiceResult<String> serviceResult = new ServiceResult<String>();
		int result = infoUserRoleDao.delete(infoUserRole);
		if (result == 1) {
			serviceResult.setCode(resultCode.get("common.sucess"));
		} else {
			serviceResult.setCode(resultCode.get("common.fail"));
		}
		return serviceResult;
	}

	@Override
	public List<InfoUserRole> getList() {
		return infoUserRoleDao.getList();
	}

	@Override
	public int getCount() {
		return infoUserRoleDao.getCount();
	}

	@Override
	public List<InfoUserRole> getByUserId(String userId) {
		return infoUserRoleDao.getByUserId(userId);
	}

	@Override
	public ServiceResult<String> saveOrUpdate(InfoUserRole infoUserRole) {
		ServiceResult<String> serviceResult = new ServiceResult<String>();
		int result = 0;
		InfoUserRole infoUserRoleT = infoUserRoleDao.getByUserIdAndRoleId(infoUserRole);
		if(infoUserRoleT==null){
			result = infoUserRoleDao.add(infoUserRole);
		}else{
			infoUserRole.setId(infoUserRoleT.getId());
			result = infoUserRoleDao.update(infoUserRole);
		}
		if (result == 1) {
			serviceResult.setCode(resultCode.get("common.sucess"));
		} else {
			serviceResult.setCode(resultCode.get("common.fail"));
		}
		return serviceResult;
	}
}
